package com.timmy.video.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.timmy.video.utils.AutoUtils;
import com.timmy.video.R;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerController;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;

import java.io.Serializable;

/**
 * @Auther: Timmy
 * @Data: 2017/6/14
 * @Fuction:
 */

public class PlayVideoActivity extends AppCompatActivity {

    private NiceVideoPlayer mNiceVideoPlayer;
    private MainActivity.VideoInfo mVideoInfo;
    private NiceVideoPlayerController mController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        AutoUtils.auto(this);//适配实际屏幕

        Intent intent = getIntent();
        Serializable s = intent.getSerializableExtra("data");
        mVideoInfo = (MainActivity.VideoInfo) s;

        init();
    }

    private void init() {
        mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
        mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.PLAYER_TYPE_NATIVE);
        mNiceVideoPlayer.setUp(mVideoInfo.filePath, null);

        mController = new NiceVideoPlayerController(this);
        mController.setTitle(mVideoInfo.title);
        mController.setTopBottomVisible(false);
        mNiceVideoPlayer.setController(mController);
        mNiceVideoPlayer.start();
        mController.setLooping(true);

        mNiceVideoPlayer.setListener(new NiceVideoPlayer.Listener() {
            @Override
            public void onStop() {
                //mController.restartd();
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mNiceVideoPlayer.isPlaying()) {
            mNiceVideoPlayer.stop();
        }

    }

    @Override
    public void onBackPressed() {
        if (NiceVideoPlayerManager.instance().onBackPressd()) {
            return;
        }
        super.onBackPressed();
    }
}
